package com.test.auth_db_auto.support.oauth2;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.test.auth_db_auto.mapper.OauthClientDetailsMapper;
import com.test.auth_db_auto.model.OauthClientDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;

/**
 * 自定义client端信息检查
 * @ Author: .Mr
 * @ ClassName BootClientDetailService
 * @ Description TODO
 * @ date 2021/12/23 11:52
 * @ Version 1.0
 */
@Service
public class BootClientDetailService implements ClientDetailsService {

    @Autowired
    private OauthClientDetailsMapper clientDetailsMapper;
    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        OauthClientDetails clientDetails = clientDetailsMapper.selectOne(Wrappers.<OauthClientDetails>lambdaQuery().eq(OauthClientDetails::getClientId, clientId));
        if (clientDetails == null) {
            throw new ClientRegistrationException("该客户端不存在");
        }
        return new BootClientDetails(clientDetails);
    }
}
